$(function() {
   $('#name').val("");
   $('#pass').val("");
   $(".input input").focus(function() {

      $(this).parent(".input").each(function() {
         $("label", this).css({
            "line-height": "18px",
            "font-size": "18px",
            "font-weight": "100",
            "top": "0px"
         })
         $(".spin", this).css({
            "width": "100%"
         })
      });
   }).blur(function() {
      $(".spin").css({
         "width": "0px"
      })
      if ($(this).val() == "") {
         $(this).parent(".input").each(function() {
            $("label", this).css({
               "line-height": "60px",
               "font-size": "24px",
               "font-weight": "300",
               "top": "10px"
            })
         });

      }
   });

   $(".button").click(function(e) {
      var pX = e.pageX,
         pY = e.pageY,
         oX = parseInt($(this).offset().left),
         oY = parseInt($(this).offset().top);
      $(this).append('<span class="click-efect x-' + oX + ' y-' + oY + '" style="margin-left:' + (pX - oX) + 'px;margin-top:' + (pY - oY) + 'px;"></span>')
      $('.x-' + oX + '.y-' + oY + '').animate({
         "width": "500px",
         "height": "500px",
         "top": "-250px",
         "left": "-250px",
      }, 600);
      $("button", this).addClass('active');
   })

   $(".alt-2").click(function() {
      if (!$(this).hasClass('material-button')) {
         $(".shape").css({
            "width": "100%",
            "height": "100%",
            "transform": "rotate(0deg)",
			"left":"0px",
			"top":"0px"
         })

         setTimeout(function() {
            $(".overbox").css({
               "overflow": "initial"
            })
         }, 600)

         $(this).animate({
            "width": "140px",
            "height": "140px",
         }, 500, function() {
            $(".box").removeClass("back");

            $(this).removeClass('active')
         });

         $(".overbox .title").fadeOut(300);
         $(".overbox .input").fadeOut(300);
         $(".overbox .button").fadeOut(300);
         $(".alt-2").addClass('material-buton');
      }

   })

   $(".material-button").click(function() {

      if ($(this).hasClass('material-button')) {
         setTimeout(function() {
            $(".overbox").css({
               "overflow": "hidden"
            })
            $(".box").addClass("back");
         }, 200)
         $(this).addClass('active').animate({
            "width": "700px",
            "height": "700px"
         });

         setTimeout(function() {
            $(".shape").css({
               "width": "5%",
               "height": "5%",
               "transform": "rotate(45deg)",
			   "left":"500px",
			   "top":"150px",
            })

            $(".overbox .title").fadeIn(300);
            $(".overbox .input").fadeIn(300);
            $(".overbox .button").fadeIn(300);
         }, 700)

         $(this).removeClass('material-button');

      }

      if ($(".alt-2").hasClass('material-buton')) {
         $(".alt-2").removeClass('material-buton');
         $(".alt-2").addClass('material-button');
      }

   });
   var flag_name=false;
   var flag_password=false;
   var flag_repassword=false;
   $('#regname').blur(function () {
      var reg = /^[a-zA-Z]{1}[a-z0-9A-Z]{5,9}$/;
      if($('#regname').val()==""){
         layer.tips("用户名不能为空",'#register-name',{tips:[2,'#ff2040'],time:1000})
         $('#register-name').css("color","#f54c53");
      }else if(!reg.test($('#regname').val())){
         layer.tips("请输入英文字母开头6-10位字符",'#register-name',{tips:[2,'#ff2040'],time:1000})
         $('#register-name').css("color","#f54c53");
      }else{
         $.getJSON('checkAccount.do',{account:$('#regname').val()},function (data) {
            console.log(data)
            if(data.code==201){
               $('#register-name').css("color","#f54c53");
               layer.tips(data.message,'#register-name',{tips:[2,'#ff2040'],time:1000})
            }else{
               $('#register-name').css("color","#00ed43");
               flag_name=true;
            }
         }
         )}
   });
   $('#regpass').blur(function () {
      if($('#regpass').val()==""){
         layer.tips("密码不能为空",'#register-password',{tips:[2,'#ff2040'],time:1000})
         $('#register-password').css("color","#f54c53");
   }else {
         $('#register-password').css("color","#00ed43");
         flag_password=true;
      }});
   $('#reregpass').blur(function () {
      if($('#reregpass').val()==""){
         layer.tips("请再次输入密码",'#register-repassword',{tips:[2,'#ff2040'],time:1000})
         $('#register-repassword').css("color","#f54c53");
      }else if($('#regpass').val() != $('#reregpass').val()){
         layer.tips("两次输入密码不一致",'#reregpass',{tips:[2,'#ff2040'],time:1000})
         $('#register-repassword').css("color","#f54c53");
      }else {
         $('#register-repassword').css("color","#00ed43");
         flag_repassword=true;
      }
   })
   $('#register').click(function () {
    if(flag_name&flag_password&flag_repassword){
       $.getJSON('register.do',{
          account:$('#regname').val(),
          password:$('#regpass').val()
       },function (data) {
          if(data.code==200){
             console.log(data.msg);
             layer.msg(data.msg,{icon:1,time:1000},function () {
                $('#shape_close').click();
             });
          }else{
             layer.msg(data.msg,{icon:2,time:1000});
          }
       })
    }else{
		$('#register i.fa').removeClass('fa-check');
		$('#register i.fa').addClass('fa-close');
	}
   })
   $('#shape_close').click(function () {
      $('#register-name').css("color","#ffffff");
      $('#register-password').css("color","#ffffff");
      $('#register-repassword').css("color","#ffffff");
      $('#regname').val("");
      $('#regpass').val("");
      $('#reregpass').val("");
      $('.overbox div label').css({
         "line-height": "60px",
         "font-size": "24px",
         "font-weight": "300",
         "top": "10px"
      })
   })
   $('#name').blur(function () {
     if($('#name').val()==""){
        layer.tips("请输入账号",'#login-username',{tips:[2,"#FFCD4C"],time:1000})
      }
   })
   $('#pass').blur(function () {
      if($('#pass').val()==""){
         layer.tips("请输入密码",'#login-password',{tips:[2,"#FFCD4C"],time:1000})
      }
   })
   $('#login').click(function () {
       $.post('login.do',{
          account:$('#name').val(),
          password:$('#pass').val()
           },function (data) {
            console.log(data.code)
            if(data.code == 200){
               sessionStorage.setItem('user',JSON.stringify(data.result));
               $('.click-efect').css('background','#2ddaed');
               $('.button.login button i.fa').removeClass('fa-close');
               $('.button.login button i.fa').addClass('fa-check');
               $('.button.login button:hover').css('border-color','#2ddaed')
               layer.msg(data.msg,{icon:1,time:800},function () {
                  location.href="index.html";
               });
            }else if(data.code == 205){
             sessionStorage.setItem('user',JSON.stringify(data.result));
             $('.click-efect').css('background','#2ddaed');
             $('.button.login button i.fa').removeClass('fa-close');
             $('.button.login button i.fa').addClass('fa-check');
             $('.button.login button:hover').css('border-color','#2ddaed')
             layer.msg(data.msg,{icon:1,time:800},function () {
                location.href="marnager.html";
             });
            }else{
				$('.button.login button i.fa').removeClass('fa-check');
				$('.button.login button i.fa').addClass('fa-close');
				$('.click-efect').css('background','#ed2d2d');
				$('.button.login button:hover').css('border-color','#ed2d2d')
            }
           },"json")
   })

});